package co.edu.usbcali.rebio.dataaccess.dao;

import co.edu.usbcali.rebio.dataaccess.sessionFactory.HibernateSessionFactory;
import co.edu.usbcali.rebio.modelo.RebCliente;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;

import org.hibernate.criterion.Example;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * A data access object (DAO) providing persistence and search support for
 * RebCliente entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class RebClienteDAO implements IRebClienteDAO {
    private static final Log log = LogFactory.getLog(RebClienteDAO.class);

    // property constants
    //public static final String  CLIAPELLIDO = "cliApellido";
    public static final String CLIAPELLIDO = "cliApellido";

    //public static final String  CLICONTACTO1 = "cliContacto1";
    public static final String CLICONTACTO1 = "cliContacto1";

    //public static final String  CLICONTACTO2 = "cliContacto2";
    public static final String CLICONTACTO2 = "cliContacto2";

    //public static final String  CLIDIRECCION = "cliDireccion";
    public static final String CLIDIRECCION = "cliDireccion";

    //public static final String  CLIEMAIL = "cliEmail";
    public static final String CLIEMAIL = "cliEmail";

    //public static final String  CLIESTADO = "cliEstado";
    public static final String CLIESTADO = "cliEstado";

    //public static final Integer  CLIIDENTIFICACION = "cliIdentificacion";
    public static final String CLIIDENTIFICACION = "cliIdentificacion";

    //public static final Integer  CLINIT = "cliNit";
    public static final String CLINIT = "cliNit";

    //public static final String  CLINOMBRE = "cliNombre";
    public static final String CLINOMBRE = "cliNombre";

    //public static final String  CLITELEFONOCONTACTO1 = "cliTelefonocontacto1";
    public static final String CLITELEFONOCONTACTO1 = "cliTelefonocontacto1";

    //public static final String  CLITELEFONOCONTACTO2 = "cliTelefonocontacto2";
    public static final String CLITELEFONOCONTACTO2 = "cliTelefonocontacto2";

    //public static final Integer  CLITELEFONOOFICINA = "cliTelefonooficina";
    public static final String CLITELEFONOOFICINA = "cliTelefonooficina";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            RebCliente Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(RebCliente instance) {
        log.debug("saving RebCliente instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            RebCliente Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(RebCliente instance) {
        log.debug("deleting RebCliente instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            RebCliente Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(RebCliente instance) {
        log.debug("updating RebCliente instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public RebCliente findById(String id) {
        log.debug("finding RebCliente instance with id: " + id);

        try {
            RebCliente instance = (RebCliente) getSession()
                                                   .get(RebCliente.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding RebCliente failed", re);
            throw re;
        }
    }

    public List<RebCliente> findByExample(RebCliente instance) {
        log.debug("finding RebCliente instance by example");

        try {
            List results = getSession().createCriteria("RebCliente")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  RebCliente entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< RebCliente> found by query
        */
    public List<RebCliente> findByProperty(String propertyName, Object value) {
        log.debug("finding RebCliente instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from RebCliente as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<RebCliente> findByCliApellido(Object cliApellido) {
        return findByProperty(CLIAPELLIDO, cliApellido);
    }

    public List<RebCliente> findByCliContacto1(Object cliContacto1) {
        return findByProperty(CLICONTACTO1, cliContacto1);
    }

    public List<RebCliente> findByCliContacto2(Object cliContacto2) {
        return findByProperty(CLICONTACTO2, cliContacto2);
    }

    public List<RebCliente> findByCliDireccion(Object cliDireccion) {
        return findByProperty(CLIDIRECCION, cliDireccion);
    }

    public List<RebCliente> findByCliEmail(Object cliEmail) {
        return findByProperty(CLIEMAIL, cliEmail);
    }

    public List<RebCliente> findByCliEstado(Object cliEstado) {
        return findByProperty(CLIESTADO, cliEstado);
    }

    public List<RebCliente> findByCliIdentificacion(Object cliIdentificacion) {
        return findByProperty(CLIIDENTIFICACION, cliIdentificacion);
    }

    public List<RebCliente> findByCliNit(Object cliNit) {
        return findByProperty(CLINIT, cliNit);
    }

    public List<RebCliente> findByCliNombre(Object cliNombre) {
        return findByProperty(CLINOMBRE, cliNombre);
    }

    public List<RebCliente> findByCliTelefonocontacto1(
        Object cliTelefonocontacto1) {
        return findByProperty(CLITELEFONOCONTACTO1, cliTelefonocontacto1);
    }

    public List<RebCliente> findByCliTelefonocontacto2(
        Object cliTelefonocontacto2) {
        return findByProperty(CLITELEFONOCONTACTO2, cliTelefonocontacto2);
    }

    public List<RebCliente> findByCliTelefonooficina(Object cliTelefonooficina) {
        return findByProperty(CLITELEFONOOFICINA, cliTelefonooficina);
    }

    /**
    * Find all RebCliente entities.
    *
    * @return List<RebCliente> all RebCliente instances
    */
    public List<RebCliente> findAll() {
        log.debug("finding all RebCliente instances");

        try {
            String queryString = "from RebCliente";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<RebCliente> findByCriteria(String whereCondition) {
        log.debug("finding RebCliente " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from RebCliente model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<RebCliente> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in RebCliente failed", re);
            throw re;
        }
    }

    public List<RebCliente> findPageRebCliente(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding RebCliente findPageRebCliente");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from RebCliente model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from RebCliente model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberRebCliente() {
        log.debug("finding RebCliente count");

        try {
            String queryString = "select count(*) from RebCliente model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
